package org.lhh.server.controller;


import org.lhh.server.annotation.LogCatch;
import org.lhh.server.annotation.UserAuth;
import org.lhh.server.common.api.JSONResult;
import org.lhh.server.common.utils.CollectionUtils;
import org.lhh.server.common.utils.IDUtils;
import org.lhh.server.common.utils.SessionUtils;
import org.lhh.server.core.codec.trans.TransMetaCodec;
import org.lhh.server.core.repository.EtlRepository;
import org.lhh.server.pojo.vo.TransVo;
import org.lhh.server.service.ITransService;
import org.pentaho.di.repository.LongObjectId;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
import org.pentaho.di.repository.RepositoryElementMetaInterface;
import org.pentaho.di.trans.TransMeta;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 辉仔
 * @since 2019-06-16
 */
@RestController
@RequestMapping("/trans")
public class TransController {

    @Autowired
    private ITransService transService;

    @UserAuth
    @LogCatch(value = "数据转换-列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public JSONResult list(@RequestParam Long directoryId) {
        List<RepositoryElementMetaInterface> transformationObjects = EtlRepository.getTransformationObjects(new LongObjectId(directoryId));
        ArrayList<TransVo> transVos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(transformationObjects)) {
            for (RepositoryElementMetaInterface repositoryElementMetaInterface : transformationObjects) {
                TransVo transVo = TransVo.encode(repositoryElementMetaInterface);
                transVo.setStatus(transService.getTransformationStatus(transVo.getName()) ? 0 : 1);
                transVos.add(transVo);
            }
        }
        return JSONResult.ok(transVos);
    }

    @UserAuth
    @LogCatch(value = "数据转换-新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JSONResult add(@RequestParam Long directoryId) {
        int i = 1;
        String name = "新建转换";
        while (EtlRepository.existTransMeta(name + i, null, new LongObjectId(directoryId))) {
            i++;
        }
        RepositoryDirectoryInterface directory = EtlRepository.getDirectory(new LongObjectId(directoryId));
        String transName = IDUtils.getId();
        TransMeta transMeta = new TransMeta();
        transMeta.setName(transName);
        transMeta.setDescription(name + i);
        transMeta.setModifiedDate(new Date());
        transMeta.setModifiedUser(SessionUtils.getUsername());
        transMeta.setRepositoryDirectory(directory);
        EtlRepository.saveTrans(transMeta);
        return JSONResult.ok(TransVo.encode(transMeta));
    }

    @UserAuth
    @LogCatch(value = "数据转换-重命名")
    @RequestMapping(value = "/rename", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult rename(@RequestParam Long id,
                             @RequestParam Long directoryId,
                             @RequestParam String name) {
        if (EtlRepository.existTransMeta(name, new LongObjectId(id), new LongObjectId(directoryId)))
            return JSONResult.errorMsg("名称已存在");
        final TransMeta transMeta = EtlRepository.loadTransformation(new LongObjectId(id));
        transMeta.setDescription(name);
        EtlRepository.saveTrans(transMeta);
        return JSONResult.ok(TransVo.encode(transMeta));
    }


    @UserAuth
    @LogCatch(value = "数据转换-获取步骤列表")
    @RequestMapping(value = "/steps", method = RequestMethod.GET)
    public JSONResult steps() {
        return JSONResult.ok(transService.steps());
    }

    @UserAuth()
    @LogCatch(value = "数据转换-获取转换")
    @PostMapping("/getTransformation")
    public JSONResult getTransformation(@RequestParam String transName,@RequestParam Long directoryId) {
        return transService.getTransformation(transName,directoryId);
    }

    @UserAuth
    @RequestMapping(value = "/getTransformationStatus/{transName}", method = RequestMethod.GET)
    public JSONResult getTransformationStatus(@PathVariable String transName) {
        return JSONResult.ok(transService.getTransformationStatus(transName));
    }

    @UserAuth
    @LogCatch(value = "数据转换-新增步骤")
    @RequestMapping(method = RequestMethod.POST, value = "/addStep")
    public JSONResult addStep(@RequestParam String graphXml, @RequestParam String pluginId, @RequestParam String name) {
        return JSONResult.ok(transService.addStep(graphXml, pluginId, name));
    }

    @UserAuth
    @LogCatch(value = "数据转换-保存转换流程")
    @RequestMapping(method = RequestMethod.POST, value = "/saveTrans")
    public JSONResult saveTransformation(@RequestParam String graphXml) {
        TransMeta transMeta = (TransMeta) TransMetaCodec.decode(graphXml);
        EtlRepository.saveTrans(transMeta);
        return JSONResult.ok();
    }

}

